package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.Map;

import javax.annotation.Resource;

import org.jackysoft.data.AbstractDataProvider;
import org.jackysoft.entity.OsTask;
import org.jackysoft.entity.User;
import org.jackysoft.mapper.OsTaskMapper;
import org.jackysoft.service.OsTaskService;
import org.jackysoft.util.Page;
import org.jackysoft.util.WorkflowDataType;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

@Service
public class OsTaskServiceImpl extends AbstractDataProvider<OsTask>
		implements OsTaskService {

	
	@Resource
	OsTaskMapper mapper;
	
	
	
	public OsTaskServiceImpl() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public Page getWorkItems(WorkflowDataType dataType,int page, int rows) {
		String table = dataType2Table(dataType);
		
		Map<String,Object> parameter = Maps.newHashMap();
		parameter.put("offset", page*rows);
		parameter.put("rows", rows);
		parameter.put("table", table);
		
		if(personal(dataType)){
			User owner = null;
			//TODO 获取当前登陆用户;
			parameter.put("owner", owner.getUsername());
			parameter.put("status", dataType.name());
		}
		
		
		
		
		final Collection<OsTask> works = mapper.findTaskItems(parameter);
		final int count = mapper.countOfTaskItems(parameter);
		Page pager = Page.build(page, count, works, true);		
		return pager;
	}

	boolean personal(WorkflowDataType dataType){		
		
		switch(dataType){
			case underway:
			case finished:
			     return true;
			case currents:			
			case histories:
          		 return false;
			default:
				 return false;			
			
		}		
		
	}
	
	String dataType2Table(WorkflowDataType dataType){
		String table = "os_currentstep";
		switch(dataType){
			case underway:
			case currents:
				table = "os_currentstep";break;
			case finished:
			case histories:
				table = "os_historystep";break;
			default:
				table = "os_currentstep";break;			
			
		}		
		return table;
	}
	
	public Collection<OsTask> watchWorkItems(long refid){
		
		Collection<OsTask> items = mapper.findWatchers(refid);
		return items;
	};

}
